//
//  HQUINavigationController.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 HZMengLiao. All rights reserved.
//
#import "THNavigationController.h"

@interface THNavigationController () <UIGestureRecognizerDelegate>

@end

@implementation THNavigationController

+ (void)initialize{
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageWithColor:ColorFromHex(0xf5f7f9)] forBarMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance] setShadowImage:[UIImage new]];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.interactivePopGestureRecognizer.delegate = self;
    self.interactivePopGestureRecognizer.enabled = YES;
    [self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}];
    
    if (@available(iOS 13.0, *)) {
        UINavigationBarAppearance *barAppearance = [[UINavigationBarAppearance alloc] init];

        if (self.navigationBar.isTranslucent) {
            barAppearance.backgroundColor = ColorFromHex(0xf5f7f9);
        } else {
            barAppearance.backgroundColor = self.navigationBar.barTintColor;
        }
        barAppearance.titleTextAttributes = self.navigationBar.titleTextAttributes;
        barAppearance.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleRegular];
        barAppearance.shadowImage = [UIImage imageWithColor:[UIColor clearColor]];
        barAppearance.backgroundImage = [UIImage imageWithColor:ColorFromHex(0xf5f7f9)];
        self.navigationBar.standardAppearance = barAppearance;
        self.navigationBar.scrollEdgeAppearance = barAppearance;
    }
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    // 防止导航控制器只有一个rootViewcontroller时触发手势
    // 过滤执行过渡动画时的手势处理
    [self.view endEditing:YES];

    if ([[self valueForKey:@"_isTransitioning"] boolValue]) {
        return NO;
    }
    BOOL haveOne  = self.childViewControllers.count == 1 ? NO : YES;
    if (!haveOne) {
        return  NO;
    }else{
        return self.disablePopEnable ? NO : YES;
    }
}
 
- (void)onDidClickBackBtn:(UIButton*)backBtn{
    [self popViewControllerAnimated:YES];
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if (self.childViewControllers.count >= 1) {
        UIImage *image = [[UIImage imageNamed:@"baseImg_back_black_big"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleDone target:self action:@selector(onDidClickBackBtn:)];
        viewController.hidesBottomBarWhenPushed = YES;
        if (@available(iOS 11.0, *)){  
            [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(-1000, 0)
                                                                forBarMetrics:UIBarMetricsDefault];
           
        }
    }
    
//    if (![viewController respondsToSelector:@selector(singleFlag)]) {
//        [super pushViewController:viewController animated:animated];
//        return;
//    }
//
//    NSMutableArray *mNavc = self.viewControllers.mutableCopy;
//
//    for (THBaseViewController *subVC in self.viewControllers) {
//        if ([subVC respondsToSelector:@selector(singleFlag)]) {
//            if ([[viewController performSelector:@selector(singleFlag)] isEqualToString:[subVC singleFlag]]) {
//                [mNavc removeObject:subVC];
//                break;//因为每次跳转到判断的话，导航盏中最多已经存在一个，查找到就break，减少事件复杂度
//            }
//        }
//    }
//    self.viewControllers = mNavc.copy;
    [super pushViewController:viewController animated:animated];
}
- (NSArray<__kindof UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated {
    // fix  https://developer.apple.com/forums/thread/660750
    if (@available(iOS 14.0, *)) {
        if (self.viewControllers.count > 1) self.topViewController.hidesBottomBarWhenPushed = NO;
    }
   NSArray<__kindof UIViewController *> *viewControllers = [super popToRootViewControllerAnimated:animated];
   return viewControllers;
}
- (UIViewController *)childViewControllerForStatusBarStyle{
    return self.topViewController;
}
- (UIViewController *)childViewControllerForStatusBarHidden{
    return self.topViewController;
}

@end
